//
//  HabitRemindAlertView.m
//  habit
//
//  Created by huck on 16/6/21.
//  Copyright © 2016年 custom. All rights reserved.
//

#import "ModifyAlertView.h"
#import "AppUtils.h"

@interface ModifyAlertView()<CustomIOSAlertViewDelegate>
{
    ModifyAlertContainer *contentView;
}

@end

@implementation ModifyAlertView

-(id)init{
    self = [super init];
    if (self) {
        [self initCustomView];
    }
    return  self;
}

-(void)initCustomView
{
    self.containerView = [self createAlertContentView];
    self.buttonTitles = @[@"取消",@"确认"];
    self.delegate = self;
    _keyboardType = InputKeyboardTypeDefault;
}

-(void)setFieldValue:(NSString *)fieldValue1
{
    _fieldValue = fieldValue1;
    contentView.fieldText = fieldValue1;
}

-(void)setTitle:(NSString *)title1
{
    _title = title1;
    contentView.titleText = title1;
}

-(void)setKeyboardType:(InputKeyboardType)keyboardType1
{
    _keyboardType = keyboardType1;
    contentView.keyboardType = keyboardType1;
}

-(void)setPlaceHolderText:(NSString *)placeHolderText1
{
    _placeHolderText = placeHolderText1;
    contentView.placeHolderText = placeHolderText1;
}

-(UIView*)createAlertContentView
{
    if(contentView==nil){
        contentView = [[[NSBundle mainBundle] loadNibNamed:@"ModifyAlertView" owner:self options:nil] firstObject];
    }
    return contentView;
}

#pragma mark CustomIOSAlertViewDelegate
- (void)customIOS7dialogButtonTouchUpInside:(id)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    ModifyAlertContainer *container = (ModifyAlertContainer *)self.containerView;
    BOOL canClose = YES;
    switch (buttonIndex) {
        case 0:
        {
            if(_modifyCancelBlock){
                canClose = _modifyCancelBlock(container.fieldText);
            }
        }
            break;
        case 1:
        {
            if(_modifyFinishBlock){
                NSString *text = [AppUtils trim:container.fieldText];
                canClose = _modifyFinishBlock(text);
            }
        }
            break;
        default:
            break;
    }
    if (canClose) {
        [self close];
    }
}

@end



/**
 *  ///////////////////内容容器////////////////////////////
 */

@interface ModifyAlertContainer()<UITextFieldDelegate>

@end

@implementation ModifyAlertContainer
@synthesize fieldText;

-(void)awakeFromNib
{
    [self initSubview];
}

-(void)initSubview
{
    _keyboardType = InputKeyboardTypeDefault;
    [_modifyField becomeFirstResponder];
}

-(void)setTitleText:(NSString *)titleText1
{
    _titleText = titleText1;
    _titleLabel.text = titleText1;
}

-(void)setFieldText:(NSString *)fieldText1
{
    fieldText = fieldText1;
    _modifyField.text = fieldText1;
}

-(NSString *)fieldText
{
    return  _modifyField.text;
}

-(void)setKeyboardType:(InputKeyboardType)keyboardType1
{
    _keyboardType = keyboardType1;
    switch (keyboardType1) {
        case InputKeyboardTypeDefault: {
            _modifyField.keyboardType = UIKeyboardTypeDefault;
            break;
        }
        case InputKeyboardTypeDecimal: {
            _modifyField.keyboardType = UIKeyboardTypeDecimalPad;
            break;
        }
        case InputKeyboardTypeNumber: {
            _modifyField.keyboardType = UIKeyboardTypeNumberPad;
            break;
        }
    }
}

-(void)setPlaceHolderText:(NSString *)placeHolderText1
{
    _placeHolderText = placeHolderText1;
    _modifyField.placeholder = placeHolderText1;
}


#pragma mark UITextFieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if(_keyboardType == InputKeyboardTypeDecimal){
        NSString *text = [NSString stringWithFormat:@"%@%@",textField.text,string];
        return  [AppUtils checkNum:text maxLen:10 afterPointMax:2];
    }
    if(_keyboardType == InputKeyboardTypeNumber){
        NSString *text = [NSString stringWithFormat:@"%@%@",textField.text,string];
        return  text.length<=11;
    }
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [_modifyField resignFirstResponder];
    return YES;
}

@end
